package com.master.equipment.service.impl;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ctg.ag.sdk.biz.AepDeviceCommandClient;
import com.ctg.ag.sdk.biz.AepDeviceCommandLwmProfileClient;
import com.ctg.ag.sdk.biz.aep_device_command.QueryCommandListRequest;
import com.ctg.ag.sdk.biz.aep_device_command.QueryCommandListResponse;
import com.ctg.ag.sdk.biz.aep_device_command_lwm_profile.CreateCommandLwm2mProfileRequest;
import com.ctg.ag.sdk.biz.aep_device_command_lwm_profile.CreateCommandLwm2mProfileResponse;
import com.master.equipment.core.AepProperties;
import com.master.equipment.service.AepService;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class AepServiceImpl implements AepService {

	private final AepProperties aepProperties;
	
	AepDeviceCommandLwmProfileClient lwmClient;
	
	AepDeviceCommandClient client;
	
	public void createLwmClient() {
		lwmClient = AepDeviceCommandLwmProfileClient.newClient().appKey(aepProperties.getAppKey())
				.appSecret(aepProperties.getAppSecret()).build();
	}

	public void createClient() {
		client = AepDeviceCommandClient.newClient().appKey(aepProperties.getAppKey())
				.appSecret(aepProperties.getAppSecret()).build();
	}
	
	@Override
	public String createCommandLwm() throws Exception {
		Map<String, Object> paras = new HashMap<>();
		paras.put("data", "070201");
		paras.put("dataLen", 3);

		Map<String, Object> commandMap = new HashMap<>();
		commandMap.put("serviceId", "alarmDectector");
		commandMap.put("method", "cmd");
		commandMap.put("paras", paras);

		Map<String, Object> map = new HashMap<>();
		map.put("deviceId", "0b6e371f45f44396858ba11fd771881e");
		map.put("operator", "李");
		map.put("productId", 17123003);
		map.put("ttl", 0);
		map.put("deviceGroupId", 100);
		map.put("level", 1);
		map.put("command", commandMap);

		CreateCommandLwm2mProfileRequest request = new CreateCommandLwm2mProfileRequest();
		request.setParamMasterKey(aepProperties.getMasterKey());
		request.setBody(JSONObject.toJSONString(map).getBytes());

		CreateCommandLwm2mProfileResponse object = lwmClient.CreateCommandLwm2mProfile(request);
		byte[] bytes = object.getBody();

		String str = new String(bytes, "UTF-8");
		System.out.println("111111111111111111111：" + str);

		client.shutdown();
		
		return str;
	}

	@Override
	public String queryCommandLwm() throws Exception {

		QueryCommandListRequest request = new QueryCommandListRequest();
		// set your request params here
		request.setParamMasterKey(aepProperties.getMasterKey()); // single value
		request.setParamProductId(17123003); // single value
		request.setParamDeviceId("0b6e371f45f44396858ba11fd771881e"); // single value
		request.setParamStartTime(null); // single value
		request.setParamEndTime(null); // single value
		request.setParamPageNow(null); // single value
		request.setParamPageSize(null); // single value

		QueryCommandListResponse object = client.QueryCommandList(request);
		byte[] bytes = object.getBody();

		String str = new String(bytes, "UTF-8");
		System.out.println("111111111111111111111：" + str);

		client.shutdown();
		
		return str;
	}

}
